#!/bin/bash

#=======================================================================
# dir-elems
# File ID: 6a061bba-0014-11e5-8931-0d8802d731e4
#
# Create sorted list with number of directory elements in a directory 
# tree. Useful to find directories with lots of files that need to be 
# split up.
#
# Author: Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later.
#=======================================================================

unset opt_recursive
test "$1" = "-r" && { opt_recursive=1; shift; }

find "$@" -type d | while read f; do
    (
        cd "$f" && {
            if test -n "$opt_recursive"; then
                echo $(find | wc -l) $f
            else
                echo $(ls -a | wc -l) $f
            fi
        }
    )
done | sort -n
